import {
  imageUrl
} from "../utils/imageutil"
import {
  Intranetnet_bx
} from "../utils/api"
Page({
  data: {
    userId: wx.getStorageSync('userInfo').id,
    imageUrl,
    type: 0,
    headList: [{
        title: '保险商城',
        type: 1
      },
      // {
      //   title: '历史保单',
      //   type: 2
      // }, 
      {
        title: '保险帮助',
        type: 3
      }
    ],
    head_curr: 1,
    baoList: [],
    programmeList: [],
    programme_curr: 0,
    programme_index: 0,
    detaulil: '',
    termList: [],
    term_curr: 2,
    term_index: 0,

    income_type: 'circle',

    auntInfo: {},
    aunt_id: '',
    order_item_date: '',

    bdList: [{
      title: '全部',
      type: 1
    }, {
      title: '已过期',
      type: 2
    }, {
      title: '近7天过保',
      type: 3
    }],
    time: '',
    overdue: [],
    joverdue: [],
    is_shop: false, //商家端进入保险

    bd_curr: 1,
    date: '',
    collapseList: [{
        title: '投保须知',
        desc: [{
          title: '1.同一个家政人员在保险有效期内仅限办理一份保险，多投无效。'
        }, {
          title: '2.该保险于生成保单后的次日零时生效。'
        }, {
          title: '3.本保单为电子保单和电子发票，没有纸质保单和纸质发票，如有需要，请下载后打印。本保单理赔方式为线上理赔流程在线跟踪，可实现快速赔付。'
        }, {
          title: '4.适用范围：18至65周岁的家政服务人员。保障期限：1月/1年。'
        }, {
          title: '5.同一个家政人员在保险有效期内仅限办理一份保险，多投无效。'
        }, {
          title: '6.请如实填写投保信息，如未履行如实告知义务的，保险公司有权依法解除保险合同，并对合同解除前发生的保险事故不承担保险责任。'
        }, {
          title: '7.本产品由中国太平洋财产保险股份有限公司承保。'
        }]
      },
      {
        title: '理赔手册',
        desc: [{
          title: '1、如需报案或理赔协助服务,请拨打全国服务电话: 400-609-9366。'
        }, {
          title: '2、报案时,请您提供保单号(或有效证件号码)、出险人姓名、出险时间、出险地点、出险原因、治疗就医情况等基本信息。'
        }, {
          title: '3、您报案后,我们将会给您发送索赔指引邮件,告知您资料要求和理赔流程。'
        }, {
          title: '4、发生合同约定保险事故需要到医院接受治疗,请到合同约定的医院(一般是二级或二级以上公立医院)进行就诊治疗。并请提醒主治医生使用医保范围内用药、诊疗项目及服务设施。就诊的同时请妥善保存病历(包含首诊病历)、原始收费凭证、处方、诊断证明、检查化验报告、住院证明等就医相关材料,以便向保险公司办理索赔申请。'
        }, {
          title: ' 5、一对一理赔协助:我们将全程协助您报案、告知您在当地如何处理、告知索赔所需相关证明资料、并监督保险公司理赔结案和协助催付赔款等。'
        }, {
          title: '6、领款方式:通过银行转账的方式,保险公司将通过财务系统直接向您所指定的银行账户进行转账支付。需要您提供开户银行、户名、账号等信息,建议您选择工农中建交或邮储,以便保险公司快速处理。'
        }]
      }, {
        title: '标准版方案',
        type: 'img',
        pic: [{
          img: 'https://sikuaiwu.oss-cn-beijing.aliyuncs.com/jiazheng/image/insurance/bzb.png'
        }]
      }, {
        title: '高级版方案',
        type: 'img',
        pic: [{
          img: 'https://sikuaiwu.oss-cn-beijing.aliyuncs.com/jiazheng/image/insurance/gjb.png'
        }]
      }, {
        title: '尊贵版方案',
        type: 'img',
        pic: [{
          img: 'https://sikuaiwu.oss-cn-beijing.aliyuncs.com/jiazheng/image/insurance/zgb.png'
        }]
      }, {
        title: '护工版方案',
        type: 'img',
        pic: [{
          img: 'https://sikuaiwu.oss-cn-beijing.aliyuncs.com/jiazheng/image/insurance/hgb.png'
        }]
      }, {
        title: '险种解释',
        type: 'img',
        pic: [{
          img: 'https://sikuaiwu.oss-cn-beijing.aliyuncs.com/jiazheng/image/insurance/xzjs1.png'
        }, {
          img: 'https://sikuaiwu.oss-cn-beijing.aliyuncs.com/jiazheng/image/insurance/xzjs2.png'
        }]
      }
    ],
    a: new Date().getFullYear(),
    b: new Date().getMonth() + 1,
    c: new Date().getDate(),
    //判断是否从订单过来的
    order_type: false,
    order_item: {},
  },
  onLoad(options) {
    //获取是否是从订单跳过来的
    if (options.order_type) {
      this.setData({
        order_type: options.order_type,
        term_curr: 2,
      })
      if (wx.getStorageSync('buy_bx')) {
        this.setData({
          order_item: wx.getStorageSync('buy_bx'),
          date: wx.getStorageSync('buy_bx').addtime.substring(0, 10),
          order_item_date: wx.getStorageSync('buy_bx').addtime.substring(0, 10)
        })
      }
    }

    if (options.is_shop) {
      this.setData({
        is_shop: true
      })
    }
    var new_year = new Date().getFullYear() // 取当前的年份
    var month = new Date().getMonth()
    var new_month = month + 1 // 取当前的月份

    var firstDay = new Date(new_year, new_month, 1) // 取当年当月中的第一天
    var lastDay = new Date(firstDay.getTime() - 1000 * 60 * 60 * 24).getDate() // 获取当月最后一天日期
    if (this.data.c == lastDay) {
      this.setData({
        b: this.data.b + 1,
        c: 1
      })
    } else {
      this.setData({
        b: this.data.b,
        c: this.data.c + 1
      })
    }
    this.setData({
      time: this.data.a + '-' + this.data.b + '-' + this.data.c
    })

    wx.removeStorageSync('get_aunt_info')
    this.getxieBao()

    if (options.aunt_id) {
      // 家政端给家政员投保
      this.setData({
        aunt_id: options.aunt_id,
        auntInfo: wx.getStorageSync('auntInfo')
      })
    }

  },

  onUnload() {
    wx.removeStorageSync('auntInfo')
    wx.removeStorageSync('buy_bx')
  },

  onShow() {
    return
    // 0添加   1更改历史
    if (wx.getStorageSync('get_aunt_info') && wx.getStorageSync('get_aunt_info').type == 0) {
      this.setData({
        auntInfo: wx.getStorageSync('get_aunt_info')
      })
    } else if (wx.getStorageSync('get_aunt_info') && wx.getStorageSync('get_aunt_info').type == 1) {
      wx.request({
        url: Intranetnet_bx + 'xieBaoCtr/adjustPolicy',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          id: this.data.order_Id,
          oldUid: this.data.old_id,
          newUid: wx.getStorageSync('get_aunt_info').toUid
        },
        success: (res => {
          if (res.data.code != 200) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })

          } else {
            this.baoList();
          }
          wx.removeStorageSync('get_aunt_info')
        })
      })
    }
  },

  //查看保单信息（下载）
  Viewdan(e) {
    wx.request({
      url: Intranetnet_bx + 'xieBaoCtr/queryOrder',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        plicyId: e.currentTarget.dataset.id
      },
      success: (res => {
        this.setData({
          detaulil: res.data.data
        })
        let url = encodeURIComponent(this.data.detaulil)
        wx.navigateTo({
          url: '/insurance/get?url=' + url,
        })
      })
    })
  },
  // 保险方案
  getxieBao() {
    getApp().globalData.posts('/index.php/api/ouyoung/oyc/sorting/ouyapi/i_list2').then(res => {
      let programmeList = res.data.data
      let termList = [{
        title: (res.data.data[0].monthMoney) + '元/月',
        money: res.data.data[0].monthMoney,
        type: 2
      }]
      if (res.data.data[0].yearMoney) {
        termList.push({
          title: (res.data.data[0].yearMoney) + '元/年',
          money: res.data.data[0].yearMoney,
          type: 2
        })
      }
      this.setData({
        programmeList,
        programme_curr: res.data.data[0].caseCode,
        termList
      })
    })
  },

  change_head_curr(e) {
    this.setData({
      head_curr: e.currentTarget.dataset.type,
    })
  },
  change_programme_curr(e) {
    let termList = [{
      title: (this.data.programmeList[e.currentTarget.dataset.index].monthMoney) + '元/月',
      money: this.data.programmeList[e.currentTarget.dataset.index].monthMoney,
      type: 2
    }]
    if (this.data.programmeList[e.currentTarget.dataset.index].yearMoney) {
      termList.push({
        title: (this.data.programmeList[e.currentTarget.dataset.index].yearMoney) + '元/年',
        money: this.data.programmeList[e.currentTarget.dataset.index].yearMoney,
        type: 2
      })
    }
    this.setData({
      programme_curr: e.currentTarget.dataset.type,
      programme_index: e.currentTarget.dataset.index,
      term_index: 0,
      termList,
    })
  },
  change_term_curr(e) {
    this.setData({
      term_curr: e.currentTarget.dataset.type,
      term_index: e.currentTarget.dataset.index,
    })
  },
  change_bd_curr(e) {
    this.setData({
      bd_curr: e.currentTarget.dataset.type,
    })
  },
  empty() {
    this.setData({
      auntInfo: {}
    })
  },
  to_get_aunt(e) {
    if (e.currentTarget.dataset.item) {
      this.setData({
        //订单id 
        order_Id: e.currentTarget.dataset.item.id,
        //原来的家政员id
        old_id: e.currentTarget.dataset.item.toUid,
        type: e.currentTarget.dataset.type
      })
    }

    wx.navigateTo({
      url: '/insurance/get_aunt?order_id=' + e.currentTarget.dataset.id + '&is_shop=' + this.data.is_shop + '&type=' + e.currentTarget.dataset.type
    })
  },
  bindDateChange(e) {
    let date = e.detail.value;
    if (this.data.order_type) {
      getApp().globalData.posts('/index.php/api/app/user/sorting/ouyoung_api/updadd?work_id=' + this.data.order_item.id + '&addtime=' + date).then(res => {
        if (res.data.code == 1) {
          this.setData({
            date
          })
        } else {
          getApp().showToast(res.data.msg)
        }
      })
    } else {
      this.setData({
        date
      })
    }
  },
  to_read(e) {
    let type = e.currentTarget.dataset.type;
    let title = e.currentTarget.dataset.title;
    if (type == 2) {
      let list = ['https://sikuaiwu.oss-cn-beijing.aliyuncs.com/jiazheng/image/insurance/1.png', 'https://sikuaiwu.oss-cn-beijing.aliyuncs.com/jiazheng/image/insurance/2.png', 'https://sikuaiwu.oss-cn-beijing.aliyuncs.com/jiazheng/image/insurance/3.png', 'https://sikuaiwu.oss-cn-beijing.aliyuncs.com/jiazheng/image/insurance/4.png']
      wx.setStorageSync('tlImageArr', list)
    } else {

    }
    wx.navigateTo({
      url: `./read?type=${type}&title=${title}`,
    })
  },

  buy() {
    if (!this.data.programme_curr) {
      wx.showToast({
        title: '请选择保险方案',
        icon: 'none'
      })
      return
    }
    if (!this.data.term_curr) {
      wx.showToast({
        title: '请选择保险期限',
        icon: 'none'
      })
      return
    }
    let baoxian_info = {
      caseCode: this.data.programmeList[this.data.programme_index].caseCode,
      caseName: this.data.programmeList[this.data.programme_index].caseName,
      monthMoney: this.data.termList[this.data.term_index].money,
      day_type: this.data.term_index == 0 ? 1 : 12,
      title: this.data.programmeList[this.data.programme_index].title,
    }
    if (this.data.aunt_id) {
      if (!this.data.date) {
        getApp().showToast('请选择开始时间')
        return
      }
      getApp().showModal('是否确认购买该保险', true).then(() => {
        this.store_bx()
      })
    } else {
      wx.setStorageSync('baoxian_info', baoxian_info)
      wx.navigateBack()
    }
  },

  store_bx() {
    getApp().globalData.posts('/index.php/api/ouyoung/oyc/sorting/ouyapi/store_bx', {
      h_id: this.data.aunt_id,
      start_day: this.data.date,
      money: this.data.termList[this.data.term_index].money,
      two_money: this.data.termList[0].money,
      caseCode: this.data.programmeList[this.data.programme_index].caseCode,
      type: this.data.income_type == 'success' ? 1 : 0,
      // type 1 = 余额抵扣
    }).then(res => {
      let data = res.data.data
      let pay_status = data.pay_status
      if (pay_status == 2) {
        getApp().showModal('投保成功').then(() => {
          wx.navigateBack()
        })
        return
      }
      wx.requestPayment({
        nonceStr: data.nonceStr,
        package: data.package,
        paySign: data.paySign,
        timeStamp: data.timeStamp,
        signType: data.signType,
        success: () => {
          getApp().showModal('投保成功').then(() => {
            wx.navigateBack()
          })
        },
        fail: (e) => {
          getApp().showToast('支付失败')
        }
      })
    })
  },

  change_income_type(e) {
    let income_type = '';
    if (e.currentTarget.dataset.type == 'success') {
      income_type = 'circle'
    } else {
      income_type = 'success'
    }
    this.setData({
      income_type
    })
  },

})